﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace 谁与争锋可视化设计
{
    public class DataGraph
    {
        const int GRAPH_HEIGHT = 320;

        //每一个坐标的间隔
        const int INTERVAL_X = 50;
        const int INTERVAL_Y = 26;

        //左边留白
        const int SPACE_X = 70;

        //上边留白
        const int SPACE_Y = 26;

        private List<System.Windows.Forms.PictureBox> m_monthHistograms;
        public List<System.Windows.Forms.PictureBox> MonthHistograms
        {
            get { return m_monthHistograms; }
            set { m_monthHistograms = value; }
        }

        private Image m_dataGraphImage;
        public Image DataGraphImage
        {
            get { return m_dataGraphImage; }
            set { m_dataGraphImage = value; }
        }

        //传入用户选择的两个时间
        public Image DrawAccordingToStatistic(DateTime startTime, DateTime endTime, Statistics statistics)
        {
            //计算出总共多少月份
            int totalMonth = (endTime.Year * 12 + endTime.Month) - (startTime.Year * 12 + startTime.Month) + 1;

            this.DrawCoordinates(totalMonth, startTime, endTime, statistics);
            return m_dataGraphImage;
        }

        //画坐标,月份总数关系到横坐标的长度
        private void DrawCoordinates(int totalMonths, DateTime startTime, DateTime endTime, Statistics statistics)
        {
            m_monthHistograms = new List<System.Windows.Forms.PictureBox>();

            int startYear = startTime.Year;
            int startMonth = startTime.Month;
            int endYear = endTime.Year;
            int endMonth = endTime.Month;

            
            int width = totalMonths * INTERVAL_X + SPACE_X;

            //创建画布
            m_dataGraphImage = new Bitmap(width, GRAPH_HEIGHT);

            //画布刷白
            Graphics g = Graphics.FromImage(m_dataGraphImage);
            SolidBrush brush = new SolidBrush(Color.Black);
            Pen mypen = new Pen(brush, 1);
            g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, GRAPH_HEIGHT);

            //画纵坐标
            g.DrawLine(mypen, SPACE_X, INTERVAL_Y, SPACE_X, GRAPH_HEIGHT - 58);
            g.DrawLine(mypen, INTERVAL_X * (totalMonths + 1) + 19, INTERVAL_Y, INTERVAL_X * (totalMonths + 1) + 19, GRAPH_HEIGHT - 58);

            int y = SPACE_Y;

            //画横线
            for (int i = 0; i < 10; i++)
            {
                g.DrawLine(mypen, SPACE_X, y, width, y);
                y = y + INTERVAL_Y;
            }

            Font font = new Font("宋体", 9);

            int x = SPACE_X + 10;

            String[] n = new String[totalMonths];
            DateTime d = startTime;
            for (int i = 0; i < totalMonths; i++)
            {       
                g.DrawString(d.Year.ToString()+ "/" + d.Month.ToString(), font, Brushes.Blue, x, 270);
                x = x + INTERVAL_X;
                d = d.AddMonths(1);
            }

            //存储每个月份的题目数
            int[] Count2 = new int[totalMonths];
            int[][] YMArray = statistics.YMArray;
            DateTime dt = startTime;

            for (int i = 0, k = 0, j = 0; i < totalMonths; i++, j++)
            {
                
                //选择日期在用户注册之前或当前时间之后
                if ((dt.Year - statistics.FirstSubmiltTime.Year) < 0 || ((dt.Year - statistics.FirstSubmiltTime.Year) == 0 && 
                    (dt.Month - statistics.FirstSubmiltTime.Month) < 0) || (dt.Year - DateTime.Now.Year) > 0 || 
                    ((dt.Year - DateTime.Now.Year) == 0) && (dt.Month - DateTime.Now.Month) > 0)
                {
                    Count2[i] = 0;                   
                }
                else
                {
                    Count2[i] = YMArray[startYear - statistics.FirstSubmiltTime.Year + k][startMonth + j - 1]; 
                }         

                if ((startMonth + j) == 12)
                {
                    j = 0;
                    k++;
                    startMonth = 0;
                }
                dt = dt.AddMonths(1);                
            }

            //记录做题最多的题数,以便绘制柱状图纵坐标时使用
            int max = 0;
            for (int i = 0; i < Count2.Length; i++)
            {
                if (Count2[i] > max)
                {
                    max = Count2[i];
                }
            }

            y = 50;

            //一个纵坐标单元
            int unitY = 1;

            int[] m = new int[8];
           
            //计算纵坐标单元
            if (max <= 8)
            {
                for (int i = 0; i < 8; i++)
                {
                    unitY = 1;
                    m[i] = 8 - i;
                }
            } 
            else
            {
                unitY = max / 8 + ((max % 8) == 0 ? 0 : 1);
                for (int i = 0; i < 8; i++)
                {
                    m[i] = unitY * (8 - i);
                }
            }
          
            for (int i = 0; i < 8; i++)
            {
                g.DrawString(m[i].ToString(), font, Brushes.Blue, 35, y);
                y = y + INTERVAL_Y;
            }

            //第三个参数改变字体风格
            Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
            SolidBrush mybrush = new SolidBrush(Color.Red);
            SolidBrush mybrush2 = new SolidBrush(Color.Green);

            x = 90;

            for (int i = 0; i < totalMonths; i++)
            {

                PictureBox pictureBox = new PictureBox();

                //dataPanel.Controls.Add(pictureBox);
                m_monthHistograms.Add(pictureBox);

                pictureBox.BackColor = System.Drawing.Color.Gray;
                pictureBox.Cursor = System.Windows.Forms.Cursors.Hand;

                //unitY相当于绘制柱状图时高度的缩放比例
                pictureBox.Location = new System.Drawing.Point(x, 260 - (int)(((double)Count2[i] / unitY) * INTERVAL_Y));
                pictureBox.Name = "pictureBox" + i;

                //
                pictureBox.Size = new System.Drawing.Size(20, (int)(((double)Count2[i] / unitY) * INTERVAL_Y));
                pictureBox.TabIndex = i;
                pictureBox.TabStop = false;
                pictureBox.Click += new System.EventHandler(this.pictureBox_Click);

                //画数值
                g.DrawString(Count2[i].ToString(), font2, Brushes.Green, x, 260 - (int)(((double)Count2[i] / unitY) * INTERVAL_Y) - 15);

                x = x + INTERVAL_X;
            }  

        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            //后期需要跳转到日期界面
            MessageBox.Show("日期界面");
        }

    }
}
